//
//  UserViewController.m
//  newDouban
//
//  Created by dllo on 15/10/8.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "LoginViewController.h"
#import "MyActivityViewController.h"
#import "MyMovieViewController.h"
#import "DataBaseSingTon.h"
@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *userTableV;

@end

@implementation UserViewController

- (void)dealloc
{
    [_userTableV release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    if (isLogin.length > 0) {
        NSLog(@"目前是登录状态");
        self.navigationItem.rightBarButtonItem.title = @"注销";
    } else {
        NSLog(@"目前是注销状态");
        self.navigationItem.rightBarButtonItem.title = @"登录";
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.tintColor = [UIColor cyanColor];
    self.navigationItem.title = @"我的";
    
    self.userTableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.userTableV.delegate = self;
    self.userTableV.dataSource = self;
    //self.userTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.userTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.userTableV];
    [_userTableV release];
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnAction:)]  autorelease];
    //self.navigationItem.rightBarButtonItem.title = @"注册";
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
}


- (void)UIAlertController:(NSString *)str
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    //注意:假如下面要加textfield要用UIAlertControllerStyleAlert
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
    //创建提示按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //block回调
        NSLog(@"点击的是确定按钮");
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:@"" forKey:@"userName1"];
        self.navigationItem.rightBarButtonItem.title = @"登录";
        if ([str isEqualToString:@"是否清除缓存"]) {
            [[DataBaseSingTon shareDataBase] dropTableAC:@"DROP TABLE user"];
            [[DataBaseSingTon shareDataBase] dropTableAC:@"DROP TABLE activityStore"];
        }
        
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //block回调
        NSLog(@"点击的是取消按钮");
    }];
    //将按钮放在提示框上
    [alertC addAction:action];
    [alertC addAction:action1];
}

- (void)rightBtnAction:(UIBarButtonItem *)sender
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    
    if (isLogin.length > 0) {
        if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"注销"]) {
            [self UIAlertController:@"是否注销"];
        } else {
            [self UIAlertController:@"你已经登录过啦!"];
        }
        
    } else {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:^{
            
        }];
}
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 5;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    if (indexPath.row == 0) {
        if (isLogin.length > 0) {
            MyActivityViewController *myActivityVC = [[MyActivityViewController alloc] init];
            [self.navigationController pushViewController:myActivityVC animated:YES];
            [myActivityVC release];
        } else {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:loginNC animated:YES completion:^{
                
            }];
        }
    }
    
    if (indexPath.row == 1) {
        if (isLogin.length > 0) {
            MyMovieViewController *myMovieVC = [[MyMovieViewController alloc] init];
            [self.navigationController pushViewController:myMovieVC animated:YES];
            [myMovieVC release];
        } else {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:loginNC animated:YES completion:^{
                
            }];
        }
    }
    
    if (indexPath.row == 2) {
        if (isLogin.length > 0) {
            [self UIAlertController:@"是否清除缓存"];
        } else {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:loginNC animated:YES completion:^{
                
            }];
        }
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
    if (0 == indexPath.row) {
        cell.textLabel.text = @"我的活动";
    }
    if (1 == indexPath.row) {
        cell.textLabel.text = @"我的电影";
    }
    if (2 == indexPath.row) {
        cell.textLabel.text = @"清除缓存";
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:24.0];
    
    
    return cell;
}


//[DataBase shareDataBasse] selectAllName:

//1.插入数据库之前判断该电影是否已经收藏过
//1.取出NSUserDefaut保存的登录用户名
//2.往数据库里插入收藏信息
// [DataBase shareDataBasse] insert: 活动model对象  name:大海

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
